package biz;

import java.sql.*;

import db.*;
import bean.User;
import security.UserSecurityBiz;

/**
 * 用户操作类
 * @author Shi Mengjie
 * @version 1.0
 */
public final class UserBiz {
	/**
	 * 通过uid获取用户的信息
	 * @param uid
	 * @return User
	 */
	public static User getUserByUid(int uid){
		DBConn dbc = new DBConn();
		User user = new User();
		Connection conn = dbc.getConn();
		PreparedStatement prepSt = null;
		ResultSet rs = null;
		String sqlQuery = "select * from users where uid = ? ";
		try{
			prepSt = conn.prepareStatement(sqlQuery);
			prepSt.setInt(1, uid);
			rs = prepSt.executeQuery();
			while (rs.next()){
				user.setUid(rs.getInt("uid"));
				user.setUname(rs.getString("uname"));
				user.setUpass(null);
				user.setUinfo(rs.getString("info"));
				user.setUemail(rs.getString("email"));
				user.initialTagAdimin();
			}
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			DBBiz.closeResultSet(rs);
			DBBiz.closePreparedStatement(prepSt);
			DBBiz.closeConnection(conn);
		}
		
		return user;
	}
	
	/**
	 * 通过uname获取用户的信息
	 * @param uname
	 * @return User
	 */
	public static User getUserByUname(String uname){
		DBConn dbc = new DBConn();
		User user = new User();
		Connection conn = dbc.getConn();
		PreparedStatement prepSt = null;
		ResultSet rs = null;
		String sqlQuery = "select * from users where uname = ? ";
		try{
			prepSt = conn.prepareStatement(sqlQuery);
			prepSt.setString(1, uname);
			rs = prepSt.executeQuery();
			while (rs.next()){
				user.setUid(rs.getInt("uid"));
				user.setUname(rs.getString("uname"));
				user.setUpass(null);
				user.setUinfo(rs.getString("info"));
				user.setUemail(rs.getString("email"));
				user.initialTagAdimin();
			}
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			DBBiz.closeResultSet(rs);
			DBBiz.closePreparedStatement(prepSt);
			DBBiz.closeConnection(conn);
		}
		
		return user;
	}
	
	/**
	 * 向数据库插入一个用户
	 * @param user
	 * @return boolean
	 */
	public static boolean insertUserByUser(User user){
		boolean flag = false;
		DBConn dbc = new DBConn();
		Connection conn = dbc.getConn();
		PreparedStatement prepSt = null;
		String sqlInsert = "insert into users(uname,upass,email,info) values(?,?,?,?) ";
		try{
			prepSt = conn.prepareStatement(sqlInsert);
			prepSt.setString(1, user.getUname());
			prepSt.setString(2, UserSecurityBiz.GenerateStorePassword(user.getUpass()));
			prepSt.setString(3, user.getUemail());
			prepSt.setString(4, user.getUinfo());
			prepSt.executeUpdate();
			flag = true;
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			DBBiz.closePreparedStatement(prepSt);
			DBBiz.closeConnection(conn);
		}
		
		return flag;
	}
	
	/**
	 * 更新用户信息：uname,email,info,
	 * @param user
	 * @return boolean
	 */
	public static boolean updateUserByUser(User user){
		boolean flag = false;
		DBConn dbc = new DBConn();
		Connection conn = dbc.getConn();
		PreparedStatement prepSt = null;
		String sqlUpdate = "update users set uname = ?, email = ? ,info = ? where uid = ?";
		try{
			prepSt = conn.prepareStatement(sqlUpdate);
			prepSt.setString(1, user.getUname());
			prepSt.setString(2, user.getUemail());
			prepSt.setString(3, user.getUinfo());
			prepSt.setInt(4, user.getUid());
			prepSt.executeUpdate();
			flag = true;
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			DBBiz.closePreparedStatement(prepSt);
			DBBiz.closeConnection(conn);
		}
		
		return flag;
	}
	
	/**
	 * 更新用户的密码，需要提供之前的密码
	 * @param uid
	 * @param prePassword
	 * @param newPassword
	 * @return boolean
	 */
	public static boolean updatePassword(int uid, String prePassword, String newPassword){
		if (!UserSecurityBiz.CheckUserByUid(uid, prePassword)) return false;
		
		boolean flag = false;
		DBConn dbc = new DBConn();
		Connection conn = dbc.getConn();
		PreparedStatement prepSt = null;
		String sqlUpdate = "update users set upass = ? where uid = ? ";
		try{
			prepSt = conn.prepareStatement(sqlUpdate);
			prepSt.setString(1, UserSecurityBiz.GenerateStorePassword(newPassword));
			prepSt.setInt(2, uid);
			prepSt.executeUpdate();
			flag = true;
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			DBBiz.closePreparedStatement(prepSt);
			DBBiz.closeConnection(conn);
		}
		
		return flag;
	}
	
	/**
	 * 管理员更新密码
	 * @param uid
	 * @param newPassword
	 * @return boolean;
	 */
	public static boolean resetPassword(int uid, String newPassword){
		boolean flag = false;
		DBConn dbc = new DBConn();
		Connection conn = dbc.getConn();
		PreparedStatement prepSt = null;
		String sqlUpdate = "update users set upass = ? where uid = ? ";
		try{
			prepSt = conn.prepareStatement(sqlUpdate);
			prepSt.setString(1, UserSecurityBiz.GenerateStorePassword(newPassword));
			prepSt.setInt(2, uid);
			prepSt.executeUpdate();
			flag = true;
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			DBBiz.closePreparedStatement(prepSt);
			DBBiz.closeConnection(conn);
		}
		
		return flag;
	}
	
	/**
	 * 删除用户id为uid的用户
	 * @param uid
	 * @param password
	 * @return boolean
	 */
	public static boolean deleteUser(int uid, String password){
		if(!UserSecurityBiz.CheckUserByUid(uid, password)) return false;
		
		boolean flag = false;
		DBConn dbc = new DBConn();
		Connection conn = dbc.getConn();
		PreparedStatement prepSt = null;
		String sqlUpdate = "delete from users where uid = ? ";
		try{
			prepSt = conn.prepareStatement(sqlUpdate);
			prepSt.setInt(1, uid);
			prepSt.executeUpdate();
			flag = true;
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			DBBiz.closeConnection(conn);
			DBBiz.closePreparedStatement(prepSt);
		}
		
		return flag;
	}
	
	/**
	 * 删除用户（用户管理用）
	 * @param uid
	 * @return boolean
	 */
	public static boolean deleteUserAdmin(int uid){
		boolean flag = false;
		DBConn dbc = new DBConn();
		Connection conn = dbc.getConn();
		PreparedStatement prepSt = null;
		String sqlUpdate = "delete from users where uid = ? ";
		try{
			prepSt = conn.prepareStatement(sqlUpdate);
			prepSt.setInt(1, uid);
			prepSt.executeUpdate();
			flag = true;
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			DBBiz.closeConnection(conn);
			DBBiz.closePreparedStatement(prepSt);
		}
		System.out.println(flag);
		return flag;
	}
	/**
	 * 检测登录信息是否匹配
	 * @param uname
	 * @param password
	 * @return boolean
	 */
	public static boolean LoginCheck(String uname,String password){
		User user = getUserByUname(uname);
		//System.out.println(uname+password);//code for debug
		return UserSecurityBiz.CheckUserByUid(user.getUid(), password);
	}
	
	public static boolean UserCheck(User user){
		return UserSecurityBiz.CheckUserByUid(user.getUid(), user.getUpass());
	}
	
	public static boolean InsertAdminUser(int uid){
		return UserSecurityBiz.InsertAdmin(uid);
	}
}
